package com.sense.feel.login.manager

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.sense.feel.login.manager.callback.OnGoogleLoginCallback
import com.sense.kukoo.lib_logger.LoggerManager

const val GOOGLE_REQUEST_CODE = 9001

object GoogleLoginManager {
    private var TAG = "GoogleLoginManager"

    /**Firebase Google登录鉴权用户信息*/
    private  var auth: FirebaseAuth?=null

    private val serverClientId =
        "34683802014-s2tdt9qrolo04ajuq6jemt0p82vg79n5.apps.googleusercontent.com"
    private val serverClientSecret = "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"

    @SuppressLint("StaticFieldLeak")
    private lateinit var googleSignInClient: GoogleSignInClient
    fun initGoogleLoginConfig(context: Context) {
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(serverClientId)
            .requestEmail()
            .build()
        googleSignInClient = GoogleSignIn.getClient(context, gso)
        auth = Firebase.auth
    }

    fun signIn(activity: Activity) {
        val signInIntent = googleSignInClient.signInIntent
        activity.startActivityForResult(signInIntent, GOOGLE_REQUEST_CODE)
    }


    fun firebaseAuthWithGoogle(
        activity: Activity,
        account: GoogleSignInAccount,
        onGoogleLoginCallback: OnGoogleLoginCallback
    ) {
        val credential = GoogleAuthProvider.getCredential(account.idToken, null)
        auth?.signInWithCredential(credential)
            ?.addOnCompleteListener(activity) { task ->
                if (task.isSuccessful) {
                    LoggerManager.e(TAG, "signInWithCredential:success")
                    val user = auth?.currentUser
                    user?.let {
                        onGoogleLoginCallback.onSuccess(it)
                    }
                    LoggerManager.e(TAG, "displayName=" + user?.displayName)
                    LoggerManager.e(TAG, "tenantId=" + user?.tenantId)
                    LoggerManager.e(TAG, "emaile=" + user?.email)
                    LoggerManager.e(TAG, "phoneNumber=" + user?.phoneNumber)
                    LoggerManager.e(TAG, "providerId=" + user?.providerId)
                    LoggerManager.e(TAG, "uid=" + user?.uid)
                    LoggerManager.e(TAG, "path=" + user?.photoUrl?.path)
                    LoggerManager.e(TAG, "authority=" + user?.photoUrl?.authority)
                    LoggerManager.e(TAG, "scheme=" + user?.photoUrl?.scheme)
                    LoggerManager.e(TAG, "port=" + user?.photoUrl?.port)
                    LoggerManager.e(TAG, "userInfo=" + user?.photoUrl?.userInfo.toString())
                    LoggerManager.e(TAG, "host=" + user?.photoUrl?.host)
                } else {

                    LoggerManager.e(TAG, "signInWithCredential:failure", task.exception)
                    onGoogleLoginCallback.onFail(task.exception.toString())
                }
            }
    }


    fun loginOut() {
        if (isGoogleLogin())
        auth?.signOut()
    }

   private fun isGoogleLogin(): Boolean {
        auth = Firebase.auth
        val user = auth?.currentUser
        return user != null
    }
}